package com.love.netty.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.love.netty.common.JSONResult;
import com.love.netty.model.WebUser;
import com.love.netty.model.req.LoginReq;
import com.love.netty.service.IWebUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @ClassName: IndexController
 * @Description:
 * @Author: wuj
 * @Date: 2021-06-02 17:35
 **/
@Controller
@Slf4j
@RequestMapping("/")
public class LoginController {

    @Autowired
    private IWebUserService iWebUserService;


    @RequestMapping(value = "login", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult login(String loginName, String password, HttpServletRequest request) {
        QueryWrapper<WebUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("login_Name", loginName).eq("password", password);
        WebUser webUser = iWebUserService.getOne(queryWrapper);
        if (webUser != null) {
            HttpSession session = request.getSession();
            session.setAttribute("loginUser",webUser);
            return JSONResult.ok(webUser.getId());
        }
        return JSONResult.errorMsg(JSONResult.NOT_FOUND_USER);
    }




}
